<template>
    <div class="home">
        <div class="home-in">
            <header>后台仓库管理系统</header>
            <div class="login">
                <input type="text" v-model="usermenu.user.id" placeholder="请输入账号"><br>
                <input type="password" v-model="usermenu.user.password" placeholder="请输入密码">
            </div>
            <div class="icode">
                <el-radio-group v-model="radio1" class="ml-4" @change="UnRadio()">
                    <el-radio label="1" size="large">管理员</el-radio>
                    <el-radio label="0" size="large">学生</el-radio>
                </el-radio-group>
            </div>
            <div class="btn">
                <button @click="goLogin">登录</button>
                <button @click="goReset">重置</button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, toRefs } from 'vue';
import { useRoute, useRouter } from "vue-router";
import { LoginApi } from "../request/api";
import { userMenu } from "../store/usermenu";
import { LoginStore } from "../store/login";

const Router = useRouter();
const Route = useRoute();

const usermenu = reactive(userMenu());
const loginId = reactive(LoginStore());

let radio1 = ref('0');
const UnRadio = () => {
    console.log(radio1.value);
}

const goLogin = async () => {
    await LoginApi({
        // name: usermenu.user.username,
        password: usermenu.user.password,
        incode: radio1.value,
        id: usermenu.user.id
    }).then(res => {
        if (res) {
            console.log(res);
            // 存储token
            if (res.token) {
                localStorage.setItem("token", res.token);
                loginId.Unidcode(res.idcode);
                ElMessage({
                    message: "登录成功！",
                    type: 'success'
                })
            }
            if (res.userMenu) {
                usermenu.addUserMenu(res.userMenu);
                let Array = userMenu().menu.map(item => {
                    return {
                        title: item.title,
                        path: item.path,
                        // component: () => modules(`../components${item.path}.vue`),
                        component: () => import(/* @vite-ignore */ '../components' + item.path + '.vue'),
                        meta: {
                            titles: item.title,
                            paths: item.path
                        }
                    }
                });
                // 添加路由表
                Array.forEach(item => {
                    Router.addRoute("layout", item)
                });
                Router.push('/')
            }
            if (res.err) {
                ElMessage({
                    message: res.err,
                    type: 'error'
                })
            }
        }
    });
}
const goReset = () => {
    usermenu.ResetUser();
}

</script>
 
<style lang = "less" scoped>
.home {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: url("../assets/login.webp");
    background-position: 100%;
    background-size: 100% 100%;

    .home-in {
        width: 450px;
        height: 340px;
        background-color: rgba(255, 255, 255, 0.6);
        border-radius: 20px;

        header {
            font-size: 20px;
            text-align: center;
            line-height: 3;
        }

        .login {
            width: 360px;
            margin: 15px auto;
            font-size: 18px;

            input {
                font-size: 16px;
                width: 100%;
                height: 40px;
                border-radius: 10px;
                margin-bottom: 20px;
                text-indent: 1em;
            }

            input[type=text]:focus {
                outline: none;
            }

            input[type=password]:focus {
                outline: none;
            }
        }

        .icode {
            display: flex;
            justify-content: center;
            height: 40px;
            align-items: center;
            margin-bottom: 10px;
        }

        .btn {
            width: 300px;
            margin: auto;
            font-size: 18px;
            display: flex;
            justify-content: space-between;

            button {
                width: 140px;
                height: 40px;
                border-radius: 10px;
                border: none;
                cursor: pointer;
            }
        }
    }
}
</style>